$(function () {
    //密码长度写在这里，方面以后修改
    let password_length = 6;
    //获取用户名
    let userName = $("input[name='username']");
    //获取密码
    let pwd = $("input[name='password']");
    let pwdaga = $("input[name='passwordAgain']");
    //用户密码的正则表达式
    // let pwd_reg = new RegExp("/^[a-zA-Z][a-zA-Z0-9_]{5,20}$/");
    //获取手机号码
    let phone = $("input[name='phone']");
    //手机号码的正则表达式
    let phone_reg = new RegExp("^1[0-9]{10}$");
    //获取邮箱
    let email = $("input[name='eamil']");
    let email_reg = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
    //获取爱好
    let hobbies = $("input[name='hobbies']");
    //获取全选、全不选、反选三个按钮
    let chooseAll = $(".hobbiesBtnBox button:nth-child(1)");
    let chooseNotAll = $(".hobbiesBtnBox button:nth-child(2)");
    let chooseAndAll = $(".hobbiesBtnBox button:nth-child(3)");
    //获取下拉窗口
    let provSelect = $("select[name='province']");
    // let provSelect1 = document.querySelector("#province");
    // let provSelect1 = document.getElementById("province");
    // console.log(provSelect1);
    // console.log(provSelect.val());
    let citySelect = $("select[name='city']");
    // console.log(citySelect);
    // let provinceArr = new Array(4);
    // provinceArr[0] = new Array("南昌市", "萍乡市", "景德镇", "吉安市", "抚州市");
    // provinceArr[1] = new Array("南京市", "苏州市", "无锡市", "常州市", "南通市");
    // provinceArr[2] = new Array("普陀区", "徐汇区", "浦东新区", "黄浦区", "嘉定区");
    // provinceArr[3] = new Array("广州市", "惠州市", "东莞市", "梅州市");

    //关于用户名的验
    $(userName).blur(function () {
        //判断
        if (userName.val().length <= 0) {
            userName.focus();
        }
    });
    //关于密码的验证
    $(pwd).blur(function () {
        //判断
        if (pwd.val().length < password_length) {
            pwd.focus();
        }
    });
    $(pwdaga).blur(function () {
        //判断
        if (pwdaga.val().length < password_length) {
            pwdaga.focus();
        }
        if (pwd.val().length >= password_length) {
            if (pwdaga.val() != pwd.val()) {
                pwdaga.focus();
                pwdaga.parent().popover().attr('data-content', '密码错误，请重新输入');
            } else {
                pwdaga.parent().popover().attr('data-content', '最少输入6个字符');
            }
        }
    });
    //关于手机号码的验证
    $(phone).blur(function () {
        //判断
        if (phone_reg.test(phone.val())) {
            phone.parent().popover().attr('data-content', '符合规则');
        } else {
            phone.focus();
            phone.parent().popover().attr('data-content', '不符合规则');
        }
        if (phone.val().length <= 0) {
            phone.parent().popover().attr('data-content', '不能为空');
        }
    });
    //关于邮箱的验证
    $(email).blur(function () {
        //判断
        if (email_reg.test(email.val())) {
            email.parent().popover().attr('data-content', '符合规则');
        } else {
            email.parent().popover().attr('data-content', '不符合规则');
        }
    });
    //关于按钮的事件
    $(chooseAll).click(function () {
        //判断
        for (let i = 0; i < hobbies.length; i++) {
            hobbies[i].checked = true;
        }
    });
    $(chooseNotAll).click(function () {
        //判断
        for (let i = 0; i < hobbies.length; i++) {
            hobbies[i].checked = false;
        }
    });
    $(chooseAndAll).click(function () {
        for (let i = 0; i < hobbies.length; i++) {
            if (hobbies[i].checked) {
                hobbies[i].checked = false;
            } else {
                hobbies[i].checked = true;
            }
        }
    });
    //关于下拉窗口的事件
    $(provSelect).blur(function () {
        if (provSelect.val() == -1) {
            provSelect.focus();
            provSelect.parent().popover().attr('data-content', '请选择省份');
        }
    });
    $(citySelect).blur(function () {
        if (citySelect.val() == -1) {
            citySelect.focus();
            citySelect.parent().popover().attr('data-content', '请选择城市');
        }
    });
    $.ajax({
        url: "data/province.json",
        method: "post",
        success: function (data) {
            $(provSelect).change(function () {
                //清空
                $(citySelect).empty();
                let provID = this.value - 1;
                //这两个结果一样
                // console.log(this.value);
                // console.log(provSelect.val());
                // console.log(data[provID].city[1]);
                if (provID == -1) {
                    $(citySelect).html("<option  value=\"-1\">--请选择--</option>");
                } else {
                    for (let i = 0; i < data[provID].city.length; i++) {
                        let ption = document.createElement("option");//创建节点
                        console.log(ption);
                        ption.innerText = data[provID].city[i];
                        $(citySelect).append(ption);
                    }
                }
            });
        },
        error: function (jqXHR) {
            //
            console.log(jqXHR.status + ":" + jqXHR.responseText);
        }
    });


    $("form").submit(function () {
        let hobbies_count = 0;
        for (let i = 0; i < hobbies.length; i++) {
            if (hobbies[i].checked) {
                hobbies_count++;
            }
        }
        if (hobbies_count != 2) {
            alert("爱好只能2个");
            return false;
        }
        hobbies_count = 0;
    });

});